package liming.ai.tool.maven;

import liming.ai.tool.AutoRegistration;
import liming.ai.tool.ToolMethod;
import liming.ai.tool.ToolRegistrationCenter;
import liming.tool.json.annotation.JsonMark;
import org.json.JSONObject;

@JsonMark("maven-java-config")
public class Maven extends AutoRegistration{
    JSONObject config;
    @Override
    protected boolean loadMethod(JSONObject config) {
        this.config = config;
        return super.loadMethod(config);
    }

    @ToolMethod(describe = "获取mavenHome,无参方法")
    public String getMavenHome(){
        return config.optString("mavenHome","未找到mavenHome");
//        return config.getString("mavenHome");
    }
    @ToolMethod(describe = "获取项目路径,无参方法")
    public String getProjectPath(){
        return config.optString("getProjectPath","未找到getProjectPath");
//        return config.getString("mavenHome");
    }
    @ToolMethod(describe = "获取maven配置文件路径,无参方法")
    public String getMavenSettingPath(){
        return config.optString("getMavenSettingPath","未找到getMavenSettingPath");
//        return config.getString("mavenHome");
    }
    @ToolMethod(describe = "获取获取jdk路径,无参方法")
    public String getJavaHome(){
        return config.optString("getJavaHome","未找到getJavaHome");
//        return config.getString("mavenHome");
    }

    @ToolMethod(name = "build", describe = "执行maven命令,传入maven生成周期状态，并获取执行结果")
    public String build(String... type){
        String cmd=String.format("%s\\bin\\mvn.cmd -f \"%s\\pom.xml\" -s \"%s\" %s -Djava.home=\"%s\""
                ,getMavenHome()
                ,getProjectPath()
                ,getMavenSettingPath()
                ,String.join(" ", type)
                ,getJavaHome());
        return ToolRegistrationCenter.invoke("cmd",  "exec", cmd).serialize().toString();
    }
}
